package com.t.codebook.utils;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.jwt.JWTValidator;
import cn.hutool.jwt.signers.JWTSignerUtil;
import org.springframework.stereotype.Component;

import java.security.*;
import java.util.HashMap;
import java.util.Map;

/**
* @Description: JWT 工具类
* @Param:
* @return:
* @Author: tangzhaofeng
* @Date: 8/26/2024
*/
@Component
public class JWTUtils {

    private static final String KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCD4s14ryET0IZQELun3c1q3N1LmWbL6XRitKZ018SBDwiyVeVdAG3xOPC9M/w9ULqBaDrySfGTj8UmP4REpbnWErdh3S+iQ6LVdJrp3celZYm9CiZOkLa1sckvLiJa1ak9s7ld2V/U1NXdCg5ijes3esl/FKHQL76I5jADHG87owIDAQAB";


    /**
    * @Description: 创建Token
    * @Param:
    * @return:
    * @Author: tangzhaofeng
    * @Date: 8/26/2024
    */
    public String createToken(HashMap<String,Object> map,Long expireTime){
        //默认的过期时间
        if (expireTime == null){
            map.put("expire_time",System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
        }
        return JWTUtil.createToken(map, KEY.getBytes());
    }


    /**
    * @Description: 校验Token
    * @Param:
    * @return:
    * @Author: tangzhaofeng
    * @Date: 8/26/2024
    */
    public void verifyToken(String token){
        JWTValidator.of(token)
                .validateAlgorithm(JWTSignerUtil.hs256(KEY.getBytes()))
                .validateDate();
    }

    /** 
    * @Description: 刷新Token
    * @Param: 
    * @return: 
    * @Author: tangzhaofeng
    * @Date: 8/26/2024
    */
    public String refreshToken(String token){
        //验证Token
        verifyToken(token);
        //刷新Token
        return token;
    }


    /**
    * @Description: Token 续期
    * @Param:
    * @return:
    * @Author: tangzhaofeng
    * @Date: 8/27/2024
    */
    public void renewalToken(String token){
        final JWT jwt = JWTUtil.parseToken(token);
        //续期
        jwt.setPayload("expire_time",System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 15);
    }



    public static void main(String[] args) {
        RSA rsa = new RSA();
        String publicKey = rsa.getPublicKeyBase64();
        System.out.println(publicKey);


    }

}
